// Copyright 2012 The Glue BSF Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview The Controller accessible from any View component.  
 *     It represents a series of functions used to communicate with the Model or entire system.
 */
goog.provide('glue.SpecController')

goog.require('glue.iController');

goog.require('goog.array');
goog.require('goog.events.EventTarget');
goog.require('goog.string');


/**
 * The controller interface passed to the views.
 * 
 * @constructor
 * @extends goog.events.EventTarget
 * 
 * @param {glue.Model} model
 * @param {glue.Controller} controller 
 */
glue.SpecController = function(model,controller){
	goog.base(this);
		
	glue.SpecController.prototype.set = goog.bind(model.set,model);
	glue.SpecController.prototype.get = goog.bind(model.get,model);
	glue.SpecController.prototype.put = goog.bind(model.put,model);
	glue.SpecController.prototype.del = goog.bind(model.del,model);
	glue.SpecController.prototype.post = goog.bind(model.post,model);
	glue.SpecController.prototype.sync = goog.bind(model.syncSend,model);
	glue.SpecController.prototype.revalidate = goog.bind(model.revalidate,model);
	glue.SpecController.prototype.conditionalRevalidate = goog.bind(model.conditionalRevalidate,model);
	glue.SpecController.prototype.clear = goog.bind(model.clear,model);
	
	glue.SpecController.prototype.load = goog.bind(controller.load,controller);
	glue.SpecController.prototype.refresh = goog.bind(controller.refresh,controller);
	glue.SpecController.prototype.close = goog.bind(controller.close,controller)
	glue.SpecController.prototype.getViews = goog.bind(controller.getViews,controller)
	
	
			
}
goog.inherits(glue.SpecController,goog.events.EventTarget)

/** 
 * Function to retrieve multiple urls as once and pass them as an array to the callback
 * 
 * @param {Array<String>} urls
 * @param {Function} cb
 */
glue.SpecController.prototype.getUrls = function(urls,cb){
	var lock = urls.length,
		result = {};
	
	var fireCallback = function(){
		var data = goog.array.map(urls,function(url){
			return result[url];
		})
		cb.apply(this||window,data);
	}
	
	goog.array.forEach(urls,goog.bind(function(url){
		this.get(url,function(data){
			lock--;
			result[url] = data;
			if(lock === 0){
				fireCallback();
			}
		})
	},this));
}

/**
 * Create function for the class.
 * 
 * @param {glue.Model} model
 * @param {glue.Controller} controller 
 */
glue.SpecController.create = function(model,controller){
	return new glue.SpecController(model,controller);
}
